#pragma  once
#include "stdafx.h"
#include "connector.h"
#include "units.h"
#include "scene.h"
#include "title.h"
//#include "sensor.h"

class QGraphicsSceneMouseEvent;
//class ConnectorClass;
class SceneView;
class Common;
class hydConPoint;
class Title;
class ParametersModel;
//class Indicator;
class hydConnector;
class Delegate;
class automaticItem;

class hydraulicItem : public QGraphicsObject
{
	Q_OBJECT
public:
	static const enum itemType_t {NODE, PIPE, VALVE, PUMP, INNER_NODE};
	hydraulicItem(SceneView* _scene, const int &_type, const QString &_name, const QPointF &_pos);
	hydraulicItem(SceneView* _scene, const int &_type);
	~hydraulicItem();

	QRectF boundingRect() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
	
	QString getStringType();
	vector<hydConPoint*> getListOfhydConPoint(){ return list_of_connection_point; }
	
	bool isAllowedManyConnectors(){ return allowedManyConnectors; }
	QRectF getSize(){ return size; }
	itemType_t getType(){ return type; }
	
	void setTitle(Title* _title);
	Title* getTitle(){ return title; }
	QString getTitleText();
	void setSize(const QRectF &_size);
	ParametersModel* getModel();
	hydConPoint* hydConPointAt(int index);
	bool typeIsNode();
	QVariant getParameter(const QString &name, const QString &script = "COMMON");
	vector<automaticItem*> getAutomaticItemList(){ return list_of_automatic_item; };
	void addAutomaticItem(automaticItem* _aut_item);
	void updateAllAutomaticItemPosition();
	QVector<double> getCoordinate();
	//void setAutomaticItem(automaticItem* _aut_item);
	void setPosition(const QPointF &_pos);
	QString getName();
private:
	SceneView* scene;	
	const itemType_t type;
	ParametersModel* model;
	Delegate* delegate;
	Title* title;
	QRectF size;
	bool resize;
	vector<automaticItem*> list_of_automatic_item;
	bool allowedManyConnectors;
	QVector<QPointF> pipe_profile;
	double pipe_length;
	void setSize();
	void drawNode(QPainter *painter);
	void drawPipe(QPainter *painter);
	void drawValve(QPainter *painter);
	void drawPump(QPainter *painter);
	void drawInnerNode(QPainter *painter);
	void init_model();
	void addHydConPoint();
	void updatehydConPointPosition();
	void displayProfileOnPipe(const QVector<QPointF> &_point_array);
	double getMinPipeLength();
	vector<hydConPoint*> list_of_connection_point;
protected:
	void mousePressEvent(QGraphicsSceneMouseEvent *event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
	void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
	void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
	void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
	void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
	QVariant itemChange(GraphicsItemChange change, const QVariant &value);
private slots:
	void getNewParameterValue(const QString &_name, const QVariant &_value);
	void update_parameters();
	void showIndicator();
	void showIndicatorLine();
	//void showSensorEditor();
	void removeItem();
	void editSensorList();
	void func(const QString &_parameter_name);
	void removeScriptParameter(const QString &script_name);
	void addScriptParameter(const QString &script_name);
public slots:
	double updateSensorPosition(automaticItem *_aut_item, const double &left_border = -1 , const double &right_border = -1 );
signals:
	void setNewParameterValue(const QString &_name, const QVariant &_value);
};

class hydConPoint : public QGraphicsObject
{
	Q_OBJECT
public:
	static const enum direction_t {ALL, UP, DOWN, LEFT, RIGHT};
	hydConPoint(SceneView* _scene, hydraulicItem* _source, const QPointF &_position_relative_source, const direction_t &_direction = ALL);
	~hydConPoint();
	direction_t direction;
	QRectF boundingRect() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
	void adjust();
	void setAccepted(bool _accepted){ accepted = _accepted; }
	bool isAccepted(){ return accepted; }
	bool isAllowedAddConnector(){ return (!connectors.size() || source->isAllowedManyConnectors()); }
	void addConnector(hydConnector* _connector){ connectors.push_back(_connector); }
	void removeConnector(hydConnector* _connector){ connectors.erase(find(connectors.begin(), connectors.end(), _connector)); }
	hydConnector* getConnector(int index){ if (!connectors.size()) return NULL; return connectors[index]; }
	void setPositionRelativeSource(QPointF _position_relative_source){ position_relative_source = _position_relative_source; adjust(); }
	QPointF getPositionRelativeSource(){ return position_relative_source; }
	QString getParentName(){ return source->getTitleText(); }
	hydraulicItem::itemType_t getParentType(){ return source->getType(); }
	int getIndexInParentsList();
    hydraulicItem* getParent(){ return source; }
	bool isChild(hydConnector* _connector);
	bool parentIsNode(){ return source->typeIsNode(); }
	direction_t getDirection(){ return direction; }
	bool isAnyDirection(){ return direction == ALL;	 }

	QPointF getDirectionPoint();
private:
	vector<hydConnector*> connectors;
	SceneView* scene;
	hydraulicItem* source;
	QPointF position_relative_source;
	bool accepted;
protected:
	void mousePressEvent(QGraphicsSceneMouseEvent *event);
signals:
	void posChanged(const QPointF&);
};
